<template>
	<view class="content">
		<u-picker 
		:show="showPicker" 
		:columns="pickerMap[pickerName]" 
		closeOnClickOverlay 
		@close="closePicker()"
		@cancel="closePicker()" 
		@confirm="confirmPicker" 
		keyName="name"></u-picker>
		<mescroll-uni ref="mescrollRef" @init="mescrollInit"  @up="upCallback" :up="upOption" :down="{use: false}">
			<view class="section-1">
				<view class="section-body">
					<view class="form">
						<view class="form-item">
							<view class="label">类别</view>
							<view class="shape"></view>
							<view class="input">
								<view class="input-body-text" @click="openPicker('type')">
									{{showName.type}}
								</view>
							</view>
						</view>
						<view class="form-item">
							<view class="label">违章类别</view>
							<view class="shape"></view>
							<view class="input">
								<view class="input-body-text" @click="openPicker('peccancy_type')">
									{{showName.peccancy_type}}
								</view>
							</view>
						</view>
						<view class="form-item">
							<view class="label">违章等级</view>
							<view class="shape"></view>
							<view class="input">
								<view class="input-body-text" @click="openPicker('lvl')">
									{{showName.lvl}}
								</view>
							</view>
						</view>
						<view class="form-item">
							<view class="label">关键字</view>
							<view class="shape"></view>
							<view class="input">
								<u--input placeholderClass="input-placeholder" class="input-body"
									v-model="form.peccancy_content"></u--input>
							</view>
						</view>
						<view class="actions">
							<view class="action" @click="handleResetForm()">
								<text>重</text>
								<text>置</text>
							</view>
							<view class="action search" @click="handleSearch()">
								<text>查</text>
								<text>询</text>
							</view>
						</view>
					</view>
					<view class="list">
						<view :class="`item ${expandList.indexOf(item.id) !== -1 ? 'item__expand' : ''}`"
							v-for="(item, index) in list" :key="item.id">
							<view class="parent">
								<view class="action" @click="expandChange(item.id)">
									<view class="iconfont icon-xiangyou1"></view>
								</view>
								<view class="name">
									{{index + 1}}.{{item.peccancy_content}}
								</view>
								<view class="problem">
									<view class="count">{{item.peccancy_level}}</text></view>
									<view class="problem-title">{{item.peccancy_type}}</view>
								</view>
							</view>
							<view class="child" v-if="expandList.indexOf(item.id) !== -1">
								<view class="child-item" v-for="(item2, idx) in item.children" :key="item2.id + idx">
									<view class="top">
										<view class="name">
											{{index + 1}}-{{idx + 1}}.{{item2.peccancy_rule}}
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</mescroll-uni>
			
	</view>
</template>

<script>
	import {
		violationListApi
	} from '@/api/person.js';
	import {
		mapGetters,
		mapActions
	} from 'vuex';
	export default {
		data() {
			return {
				showName: {
					type: '全部',
					peccancy_type: '全部',
					lvl: '全部',
				},
				form: {
					type: '',
					peccancy_type: '',
					lvl: '',
					peccancy_content: '',
				},
				mescroll: null, // mescroll实例对象 (此行可删,mixins已默认)
				upOption: {
					textNoMore: '-- 没有更多了 --'
				},
				pageSize: 20,
				list: [],
				expandList: [],
				pickerName: '',
				showPicker: false,
				pickerMap: {
					type: [
						[{
								id: 1,
								name: '全部',
								type: ''
							},
							{
								id: 2,
								name: '输电',
								type: '2'
							},
							{
								id: 3,
								name: '变电',
								type: '3'
							},
							{
								id: 4,
								name: '配电',
								type: '4'
							},
							{
								id: 5,
								name: '营销',
								type: '5'
							},
							{
								id: 6,
								name: '调控及自动化专业',
								type: '6'
							},
						]
					],
					peccancy_type: [
						[{
								id: 1,
								name: '全部',
								type: ''
							},
							{
								id: 2,
								name: '管理类',
								type: '2'
							},
							{
								id: 3,
								name: '行为类',
								type: '3'
							},
							{
								id: 4,
								name: '装置类',
								type: '4'
							},
						]
					],
					lvl: [
						[{
								id: 1,
								name: '全部',
								type: ''
							},
							{
								id: 2,
								name: 'Ⅰ',
								type: '2'
							},
							{
								id: 3,
								name: 'Ⅱ',
								type: '3'
							},
							{
								id: 4,
								name: 'Ⅲ',
								type: '4'
							},
							{
								id: 5,
								name: 'Ⅳ',
								type: '4'
							},
							{
								id: 6,
								name: 'Ⅴ',
								type: '4'
							},
						]
					],
				},
			};
		},
		onLoad() {
			this.violationList()
		},
		methods: {
			/*mescroll组件初始化的回调,可获取到mescroll对象 (此处可删,mixins已默认)*/
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			async expandChange(id, project_id) {
				const idx = this.expandList.indexOf(id);
				if (idx !== -1) {
					this.expandList.splice(idx, 1);
				} else {
					this.expandList.push(id);
				}
				if (project_id) {
					if (!this.list[id.split('-')[0]].children[id.split('-')[1]].children) {
						this.fetchInfo(id, project_id)
					}
				}
			},
			async fetchInfo(id, project_id) {
				uni.showLoading({title: '加载中...'});
				const { data } = await projectRiskByProjectIdApi({id: project_id});
				if (data.code === 1) {
					let dep = JSON.parse(JSON.stringify(this.list));
					dep[id.split('-')[0]].children[id.split('-')[1]].children = data.data;
					this.list = dep;
				}
				uni.hideLoading();
			},
			/*上拉加载的回调*/
			async upCallback(page) {
				const {data} =  await violationListApi({
					...this.form,
					page: page.num,
					pageSize: this.pageSize,
				});
				
				if (data.code === 1 || data.code === 200) {
					let res = data.data.data;
					let arr = [];
					res.forEach((item)=> {
					  let children = []
					  item.children = [{
						  peccancy_rule: item.peccancy_rule
					  }]
					  arr.push(item);
					});
					// 接口返回的当前页数据列表 (数组)
					let curPageData = arr; 
					// 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
					let curPageLen = curPageData.length; 
					// 接口返回的总页数 (如列表有26个数据,每页10条,共3页; 则totalPage=3)
					let totalPage = data.data.data.last_page; 
					//设置列表数据
					if(page.num == 1) this.list = []; //如果是第一页需手动置空列表
					this.list = this.list.concat(curPageData); //追加新数据
					// 请求成功,隐藏加载状态
					this.mescroll.endByPage(curPageLen, totalPage); 
					setTimeout(()=>{
						this.mescroll.endSuccess(curPageLen);
					},20);
				} else{
					//  请求失败,隐藏加载状态
					this.mescroll.endErr()
				}
			},
			async violationList() {
				uni.showLoading({title: '加载中...'});
				let j = await violationListApi({
					...this.form
				})
				if (j.code = 1) {
					uni.hideLoading();
					this.listData = j.data.data.data
				}
			},
			openPicker(type) {
				this.pickerName = type;
				this.showPicker = true;
			},
			closePicker() {
				this.showPicker = false;
			},
			confirmPicker(e) {
				const {
					value
				} = e;
				const item = value[0]; 
				this.form[this.pickerName] = item.name == '全部'? '': item.name;
				this.showName[this.pickerName] = item.name;
				this.closePicker();
			},
			handleResetForm() {
				// for (let key in this.form) {
				// 	this.form[key] = '';
				// }
				// for (let key in this.showName) {
				// 	this.showName[key] = '';
				// }
				this.form.peccancy_content = '';
				this.mescroll.resetUpScroll();
				this.mescroll.scrollTo(0,0);
				
			},
			handleSearch() {
				this.mescroll.resetUpScroll();
				this.mescroll.scrollTo(0,0);
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		background-color: #fff;
	}

	.section-1 {
		position: relative;
		z-index: 10;
		background-color: #3a5df5;
		.section-body{
			.list{
				
				width: 670rpx;
				box-sizing: border-box;
				margin-left: 40rpx;
				border-top: 2rpx solid #d8e4f2;
			
				.item {
					width: 100%;
					border-bottom: solid 2rpx #f2f2f0;
			
					.parent {
						width: 100%;
						@include clear();
						.name {					
							width: 380rpx;
							line-height: 30rpx;
							padding: 26rpx 0;
							font-size: 26rpx;
							font-weight: normal;
							font-stretch: normal;
							letter-spacing: 2rpx;
							color: #3b3c40;
							float: left;
						}
						.problem{
							width: 200rpx;
							height: 46rpx;
							line-height: 46rpx;
							font-size: 16rpx;
							color: #3b3c40;
							float: right;
							.problem-title{
								font-size: 16rpx;
								color: #3b3c40;
								float: right;
								height: 40rpx;
								line-height: 40rpx;
								margin-top: 18rpx;
								background: #395cf5;
								color: white;
								margin-right: 10rpx;
								padding: 3rpx 8rpx;
								border-radius: 28rpx;
							}
							.count {
								float: right;
								min-width: 46rpx;
								height: 46rpx;
								margin-top: 18rpx;
								border-radius: 28rpx;
								line-height: 46rpx;
								text-align: center;
								font-size: 23rpx;
								font-weight: bold;
								color: #395cf5;
								background: red;
								color: white;
								text {
									font-size: 16rpx;
								}
							}
						}
						
			
						.action {
							float: left;
							width: 32rpx;
							margin-right: 18rpx;
							line-height: 82rpx;
							text-align: center;
							color: #3b3c40;
			
							.iconfont {
								font-size: 32rpx;
								transition: 0.2s ease;
								transform: rotate(90deg);
							}
			
						}
					}
					.child {
						width: 750rpx;
						margin-left: - 40rpx;
						background: #f2f2fa;
			
						.child-item {
							box-sizing: border-box;
							padding-left: 50rpx;
							width: 100%;
							border-bottom: 2rpx solid #e3e8f5;
							box-sizing: border-box;
							padding-right: 40rpx;
							@include clear();
							&:last-child {
								border-bottom: none;
							}
							
							
							.top {
								width: 100%;
								overflow: hidden;
								box-sizing: border-box;
								padding-left: 40rpx;
			
								.name {
									float: left;
									width: calc(100% - 100rpx);
									box-sizing: border-box;
									padding: 26rpx 0;
									font-size: 26rpx;
									color: #3b3c40;
									font-size: 26rpx;
								}
								.problem{
									width: 200rpx;
									height: 30rpx;
									line-height: 46rpx;
									font-size: 16rpx;
									color: #3b3c40;
									float: right;
									.count {
										float: right;
										min-width: 46rpx;
										height: 46rpx;
										margin-top: 18rpx;
										border-radius: 28rpx;
										line-height: 46rpx;
										text-align: center;
										font-size: 16rpx;
										color: #3b3c40;
										text {
											font-size: 16rpx;
										}
									}
									.problem-title{
										font-size: 16rpx;
										color: #3b3c40;
										float: right;
										height: 46rpx;
										line-height: 46rpx;
										margin-top: 18rpx;
									}
								}
			
								.action {
									width: 32rpx;
									line-height: 82rpx;
									float: left;
									text-align: center;
									color: #7f98ae;
									margin-right: 18rpx;
									.iconfont {
										font-size: 32rpx;
										transition: 0.2s ease;
										transform: rotate(90deg);
									}
								}
							}
						}
					}
					
					.grad{
						width: 100%;
						background: #d9d9e2;
						.grad-item{
							width: 100%;
							border-bottom: 2rpx solid #e3e8f5;
							
							&:last-child {
								border-bottom: none;
							}
							
							.grad-top {
								width: 100%;
								overflow: hidden;
								box-sizing: border-box;
								padding-left: 40rpx;
								.name {
									float: left;
									width: calc(100% - 40rpx);
									padding: 26rpx 0;
									padding-bottom: 10rpx;
									font-size: 26rpx;
									color: #3b3c40;
									padding-left: 50rpx;
									box-sizing: border-box;
								}
								.desc{
									float: left;
									width: calc(100% - 40rpx);
									font-size: 16rpx;
									color: #9b9b9b;
									box-sizing: border-box;
									padding-left: 50rpx;
									padding-bottom: 26rpx;
									text{
										min-width: 300rpx;
										margin-right: 30rpx;
										display: inline-block;
									}
								}
							}
						}
					}
					.action__active{
						.iconfont{
							transform: rotate(-90deg)!important;
						}
					}
				}
			
				.item__expand {
						.parent {
							.action {
								.iconfont {
									transform: rotate(-90deg);
								}
							}
						}
				
						.child {
							box-shadow: 0rpx 10rpx 10rpx 0rpx rgba(0, 0, 0, 0.05) inset;
						}
					}
				
			}
			.btn{
				text-align: center;
				width: 100rpx;
				height: 30rpx;
				border-radius: 21rpx;
				font-size: 20rpx;
				font-weight: normal;
				font-stretch: normal;
				line-height: 30rpx;
				letter-spacing: 0rpx;
				color: white;
				background-color: #1534ce;
				padding: 2rpx;
				margin-right: 2rpx;
			}
			.btn-color{
				width: 40rpx;
				background-color: red;
				margin-left: 5rpx;
			}
			.collapse-item{
				color: #717171;
				font-size: 20rpx;
			}
			background-color: #fff;
			border-top-left-radius: 50rpx;
			border-top-right-radius: 50rpx;
		}
		.list {
			width: 670rpx;
			box-sizing: border-box;
			margin-left: 40rpx;
			border-top: 2rpx solid #d8e4f2;

			.item {
			
				width: 100%;
				border-bottom: solid 2rpx #f2f2f0;
					
				.parent {
					width: 100%;
					@include clear();
					.name {					
						width: calc(100% - 230rpx);
						line-height: 30rpx;
						padding: 26rpx 0;
						font-size: 26rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 2rpx;
						color: #3b3c40;
						float: left;
					}
					
					.type {
						padding: 26rpx 0;
						float: right;
						text{
							padding: 0 20rpx;
							height: 30rpx;
							border-radius: 15rpx;
							border: solid 2rpx #1534ce;
							font-size: 16rpx;
							line-height: 32rpx;
							display: inline-block;
							color: #1534ce;
							margin-left: 15rpx;
							float: right;
						}
					}
					.level {
						padding: 26rpx 0;
						float: right;
						text{
							height: 30rpx;
							width: 30rpx;
							border: solid 2rpx #ce1515;
							border-radius: 50%;
							font-size: 16rpx;
							line-height: 32rpx;
							text-align: center;
							display: inline-block;
							background-color: #ce1515;
							color: #fff;
							float: right;
							margin-left: 15rpx;
						}
					}
					
					.action {
						float: left;
						width: 32rpx;
						margin-right: 18rpx;
						line-height: 82rpx;
						text-align: center;
						color: #3b3c40;
					
						.iconfont {
							font-size: 32rpx;
							transition: 0.2s ease;
							transform: rotate(90deg);
						}
					
					}
				}
				.child {
					width: 750rpx;
					margin-left: - 40rpx;
					background: #f2f2fa;
					
					.child-item {
						
						width: 100%;
						border-bottom: 2rpx solid #e3e8f5;
						box-sizing: border-box;
						padding-left: 50rpx;
						@include clear();
						&:last-child {
							border-bottom: none;
						}
						
						
						.top {
							width: 100%;
							overflow: hidden;
							box-sizing: border-box;
							padding-left: 40rpx;
					
							.name {
								float: left;
								width: 100%;
								box-sizing: border-box;
								padding: 26rpx 0;
								font-size: 20rpx;
								color: #3b3c40;
								color: #7f98ae;
								padding-right: 40rpx;
							}
							.desc{
								float: left;
								width: 100%;
								font-size: 16rpx;
								color: #9b9b9b;
								box-sizing: border-box;
								padding-left: 50rpx;
								padding-bottom: 26rpx;
								padding-right: 75rpx;
								text{
									padding: 0 20rpx;
									height: 30rpx;
									border-radius: 15rpx;
									border: solid 2rpx #1534ce;
									font-size: 15rpx;
									line-height: 30rpx;
									display: inline-block;
									color: #1534ce;
									margin-left: 25rpx;
									float: right;
								}
							}
							.action {
								width: 32rpx;
								line-height: 82rpx;
								float: left;
								text-align: center;
								color: #7f98ae;
								margin-right: 18rpx;
								.iconfont {
									font-size: 32rpx;
									transition: 0.2s ease;
									transform: rotate(90deg);
								}
							}
						}
					}
				}
				
				.grad{
					width: 100%;
					background: #d9d9e2;
					.grad-item{
						width: 100%;
						border-bottom: 2rpx solid #e3e8f5;
						
						&:last-child {
							border-bottom: none;
						}
						
						.grad-top {
							width: 100%;
							overflow: hidden;
							box-sizing: border-box;
							padding-left: 40rpx;
							.name {
								float: left;
								width: calc(100% - 40rpx);
								padding: 26rpx 0;
								padding-bottom: 10rpx;
								font-size: 26rpx;
								color: #3b3c40;
								padding-left: 50rpx;
								box-sizing: border-box;
							}
							.desc{
								float: left;
								width: calc(100% - 40rpx);
								font-size: 16rpx;
								color: #9b9b9b;
								box-sizing: border-box;
								padding-left: 50rpx;
								padding-bottom: 26rpx;
								text{
									min-width: 300rpx;
									margin-right: 30rpx;
									display: inline-block;
								}
							}
						}
					}
				}
				.action__active{
					.iconfont{
						transform: rotate(-90deg)!important;
					}
				}
				
			}
		}

		.actions {
			width: 100%;
			height: 50rpx;
			margin-top: 29rpx;
			padding-bottom: 40rpx;
			display: flex;
			justify-content: space-between;

			.action {
				width: 288rpx;
				height: 48rpx;
				background-color: #395cf5;
				border-radius: 20rpx;
				text-align: center;
				line-height: 48rpx;
				font-size: 26rpx;
				font-weight: normal;
				font-stretch: normal;
				color: #ffffff;

				text {
					&:first-child {
						margin-right: 20rpx;
					}
				}
			}

			.search {
				background-color: #1534ce;
			}
		}

		.form {
			width: 100%;
			padding: 0 75rpx;
			padding-top: 40rpx;
			box-sizing: border-box;

			.form-item {
				width: 600rpx;
				height: 48rpx;
				border-radius: 20rpx;
				border: solid 2rpx #1534ce;
				margin: 0 auto;
				margin-bottom: 34rpx;
				@include clear();

				.label {
					float: left;
					width: 134rpx;
					height: 48rpx;
					text-align: center;
					line-height: 48rpx;
					font-size: 26rpx;
					font-weight: normal;
					font-stretch: normal;
					letter-spacing: 0rpx;
					color: #1534ce;
				}

				.shape {
					width: 3rpx;
					height: 32rpx;
					background-color: #7f98ae;
					float: left;
					margin-top: 8rpx;
				}

				.input {
					float: left;
					width: calc(100% - 134rpx - 3rpx);

					.input-body-text {
						box-sizing: border-box;
						width: calc(100% - 80rpx);
						height: 48rpx;
						line-height: 48rpx;
						font-size: 26rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 0rpx;
						color: #1534ce;
						text-align: center;
						text-overflow: ellipsis;
						overflow: hidden;
						white-space: nowrap;
					}

					.input-body {
						box-sizing: border-box;
						width: 100%;
						height: 44rpx;
						padding: 0 !important;
						border: none;
						padding-left: 30rpx !important;
						padding-right: 30rpx !important;
						font-size: 26rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 0rpx;
						color: rgba(0, 0, 0, 0.7);
					}

					.input-placeholder {
						font-size: 26rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 0rpx;
						color: rgba(0, 0, 0, 0.5);
					}
				}
			}
		}
	}

	.section-0 {
		width: 100%;
		background-color: #2e5efe;

		.section-body {
			width: 100%;
			padding-left: 40rpx;
			height: 230rpx;
			padding-right: 40rpx;
			padding-bottom: 50rpx;
			background-color: #1534ce;
			background-size: cover;
			background-repeat: no-repeat;
			border-top-left-radius: 50rpx;
			border-top-right-radius: 50rpx;
			overflow: hidden;
			box-sizing: border-box;

			.item {
				width: 33.33%;
				float: left;
				display: flex;
				flex-wrap: wrap;
				align-content: space-between;
				padding-top: 40rpx;
				height: 100rpx;
				position: relative;

				.shape {
					width: 4rpx;
					height: 84rpx;
					background-image: linear-gradient(rgba(#3658f2, 0.3) 0%, #3658f2 30%, #3658f2 70%, rgba(#3658f2, 0.3) 100%, );
					position: absolute;
					right: 0;
					top: 48rpx;
				}

				.top {
					width: 100%;
					text-align: center;
					font-size: 16rpx;
					font-weight: normal;
					font-stretch: normal;
					letter-spacing: 0rpx;
					color: #95c4fa;
				}

				.bottom {
					width: 100%;
					text-align: center;
					font-size: 58rpx;
					font-weight: bold;
					font-stretch: normal;
					letter-spacing: 0rpx;
					color: #ffde00;
				}
			}
		}
	}
</style>
